package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.commonutils.vo.CourseWebOrderVO;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVO;
import com.atguigu.eduservice.entity.vo.CourseQueryVO;
import com.atguigu.eduservice.entity.vo.CourseWebVO;
import com.atguigu.eduservice.entity.vo.PublishCourseInfoVO;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zr
 * @since 2021-11-12
 */
@RestController
@Api(tags = "课程管理")
@RequestMapping("/edu/service/course")
//@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;



    // 课程列表  TOOD 完善条件查询带分页
    @ApiOperation(value = "分页课程列表")
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public Result pageCourseCondition(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable long limit,
            @ApiParam(name = "CourseQueryVO", value = "条件查询封装", required = true)
            @RequestBody CourseQueryVO courseQueryVO) {
        // 创建条件对象
        Page<EduCourse> eduCoursePage = new Page<>(current, limit);
        eduCourseService.getCourseConditionInfo(eduCoursePage, courseQueryVO);
        return Result.ok(eduCoursePage);
    }

    // 根据课程Id删除课程
    @ApiOperation(value = "根据ID删除课程")
    @DeleteMapping("deleteCourse/{courseId}")
    public Result deleteCourse(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId) {
        boolean flag = eduCourseService.removeCourseById(courseId);
        return flag ? Result.ok().message("删除成功") : Result.fail().message("删除失败");
    }

    @ApiOperation(value = "新增课程")
    @PostMapping("addCourseInfo")
    public Result addCourseInfo(
            @ApiParam(name = "CourseInfoVO", value = "课程基本信息", required = true)
            @RequestBody CourseInfoVO courseInfoVO) {
        String id = eduCourseService.saveCourseInfo(courseInfoVO);
        return Result.ok(id);
    }

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping("getCourseInfo/{courseId}")
    public Result getCourseInfo(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId) {
        CourseInfoVO courseInfoVO = eduCourseService.getCourseInfo(courseId);
        return Result.ok(courseInfoVO);
    }

    @ApiOperation(value = "更新课程")
    @PostMapping("updateCourseInfo")
    public Result updateCourseInfo(@RequestBody CourseInfoVO courseInfoVO) {
        eduCourseService.updateCourseInfo(courseInfoVO);
        return Result.ok();
    }

    // 根据课程Id查询课程确认信息
    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("getPublishCourseInfo/{courseId}")
    public Result getPublishCourseInfo(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId) {
        PublishCourseInfoVO pub = eduCourseService.getPublishCourseInfo(courseId);
        return Result.ok(pub);
    }

    // 根据课程Id修改课程状态 课程最终发布
    @ApiOperation(value = "根据id发布课程")
    @PostMapping("publishCourse/{courseId}")
    public Result publishCourse(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return Result.ok();
    }
}

